/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import model.University;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Tina
 */

public class Universitymanagement {
    public int uniNr;

    public Universitymanagement() {
    }
   
        University university = new University();
        
        public void readUniversity() throws IOException, SAXException{
            
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        
        try {
           DocumentBuilder builder = factory.newDocumentBuilder();
           Document doc = builder.parse("./web/universityXML_1.xml");
           NodeList continentList = doc.getElementsByTagName("Continent");
           
           //countryNr = countryList.getLength();
           
            for(int i=0; i<continentList.getLength(); i++){
                Node continentNode = continentList.item(i);
                System.out.println(continentNode.getTextContent());
                
               
            }      
        } catch(ParserConfigurationException ex) { 
            ex.printStackTrace();
        } catch(SAXException ex) {
            ex.printStackTrace();
        } catch(IOException ex){
            ex.printStackTrace();
        }
 
     } 
     
     public void addUniversity(String continent, String country, String city)throws IOException, SAXException{
            
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        
        try {
           DocumentBuilder builder = factory.newDocumentBuilder();
           Document doc = builder.parse("./web/universityXML_1.xml");
           NodeList continentList = doc.getElementsByTagName(continent);
           Node continentNode = continentList.item(0);
           NodeList countryList = continentNode.getChildNodes();
          
              
               System.out.println(countryList.getLength());
               
               for(int j=0;j<countryList.getLength();j++){
                   Node countryNode = countryList.item(j);
                   
                   String countryName = countryNode.getNodeName();
                   System.out.println("Country:" + countryName);
                   if(countryName.equals(country)){
                       
                   System.out.println("Country exists:" + countryNode.getNodeName());
                   
                   NodeList cityList = countryNode.getChildNodes();
                       
                        for(int k=0;k<cityList.getLength();k++){
                            Node cityNode = cityList.item(k);
                            System.out.println(cityList.getLength());
                           
                            if(cityNode.getNodeName().contains(city)){
                                System.out.println("City exists" + cityNode.getNodeName());
                                 Node uniNew = doc.createElement("University");
                                 cityNode.appendChild(uniNew);
                                 uniNew.setTextContent("New uni");
                                 System.out.println(uniNew.getTextContent());
                              break;
                            }else{
                                System.out.println("City does not exist!!!");
                                Node cityNew = doc.createElement(city);
                                countryNode.appendChild(cityNew);
                                
                                Node uniNew = doc.createElement("University");
                                 cityNew.appendChild(uniNew);
                                 uniNew.setTextContent("New uni");
                                 System.out.println(uniNew.getTextContent());
                            }
                    
                        }
                        break;
                   }else{
                       System.out.println("Country does not exist!!");
                       Node countryNew = doc.createElement(country);
                       continentNode.appendChild(countryNew);
                       Node cityNew = doc.createElement(city);
                                countryNew.appendChild(cityNew);
                                
                                Node uniNew = doc.createElement("University");
                                 cityNew.appendChild(uniNew);
                                 uniNew.setTextContent("New uni");
                   }
               }
          this.readUniversity();  
        } catch(ParserConfigurationException ex) { 
            ex.printStackTrace();
        } catch(SAXException ex) {
            ex.printStackTrace();
        } catch(IOException ex){
            ex.printStackTrace();
        }
     }

    public int getUniNr() {
        return uniNr;
    }

    public void setUniNr(int uniNr) {
        this.uniNr = uniNr;
    }

    public University getUniversity() {
        return university;
    }

    public void setUniversity(University university) {
        this.university = university;
    }      
}
